package com.wuba.rxjavademo.tzx.map;

import com.wuba.rxjavademo.tzx.common.Base;
import com.wuba.rxjavademo.tzx.common.SimpleSubscriber;

import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by tanzhenxing
 * Date: 2016/6/27.
 * Description:
 */
public class RxMap extends Base {
    public static void main(String[] args) {
        RxMap map = new RxMap();
        map.run();
    }

    @Override
    public void run() {
        getObservable()
                .map(new Func1<Integer, Integer>() {
                    @Override
                    public Integer call(Integer integer) {
                        if (integer < 4) {
                            return 11;
                        }
                        throw new IllegalStateException("4 is error~!");
                    }
                })
                .doOnNext(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Base.print("doOnNext:" + integer);
                    }
                })
                .doOnError(new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Base.print("doOnError");
                    }
                })
                .doOnCompleted(new Action0() {
                    @Override
                    public void call() {
                        Base.print("doOnCompleted");
                    }
                })
                .subscribe(new SimpleSubscriber<Integer>(){
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        super.onNext(integer);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }
}
